package com.studentexam.student.controller;

import com.studentexam.student.model.Exam;
import com.studentexam.student.service.ExamService;
import com.studentexam.student.tool.ResponseMessage;
import com.studentexam.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="exam")
@CrossOrigin
@RestController
public class ExamController {


    @Autowired
    private ExamService examService;

    @ApiOperation(value = "试题信息添加方法")
    @RequestMapping(value = "/addExam", method = RequestMethod.GET)
    public ResponseMessage add(Exam exam) {
        int isAdd = 0;
//        1.判断是否空对象
        if (exam != null) {
//               2.添加数据
            isAdd = examService.addExam(exam);
            if (isAdd==1){
                return Result.success("保存成功");
            }else if (isAdd==-1){
                return Result.success("已重复，不允许添加");
            }else return Result.error("保存失败");
        }
        return Result.error("保存失败");
    }


    @ApiOperation(value = "试题信息删除方法")
    @RequestMapping(value = "/delExam", method = RequestMethod.GET)
    public ResponseMessage delete(int examid){
        if (examid!=0){
            int isDel=0;
            isDel=examService.delExam(examid);
            if (isDel>0){
                return Result.success("删除试题信息成功");
            }else return Result.error("删除试题信息失败");
        }else return Result.error("删除失败，试题编号不能为0");
    }

    @ApiOperation(value = "试题信息更新方法")
    @RequestMapping(value = "/updateExam")
    public ResponseMessage update(Exam exam) {
        if (exam != null) {
            if (exam.getExamid()!= 0) {
                if (examService.updateExam(exam)> 0) {
                    return Result.success("更新试题信息成功");
                } else return Result.error("更新试题信息失败");
            } else return Result.error("更新试题信息失败，试题编号不能为0");
        } else return Result.error("更新试题信息失败，此对象不能为空");
    }

    @ApiOperation(value = "根据条件获取试题信息")
    @RequestMapping(value = "/getByExamid",method = RequestMethod.GET)
    public ResponseMessage getByExamid(int examid){
        Map<String, List> map=new HashMap<>();
        List<Exam> examList=examService.getByExamid(examid);
        map.put("list",examList);
        return Result.success("查询成功",map);
    }
}
